#!/usr/bin/perl

my $SYSVERS = "$ARGV[3]"."/System/Library/CoreServices/SystemVersion.plist";
##################

sub CheckVersion
{
    my $path            = $_[0];
    my $version         = $_[1];
    my $keyName         = $_[2];
    my $operator        = $_[3];

    if (! -e $path) {
        return 0;
    }

    if (!$operator) {
        $operator = "==";
    }

    my $oldSeperator = $/;
    $/ = \0;

    open( PLIST, "$path") || do {
        return 0;
    };

    $plistData = <PLIST>;
    $plistData =~ /<dict>(.*?)<\/dict>/gis;

    @items = split(/<key>/, $plistData);

    shift @items;
    foreach $item (@items) {
        $item =~ /(.*?)<\/key>.*?<string>(.*?)<\/string>/gis;
        $versiondata{ $1 } = $2;
    }

    close(PLIST);

    $/ = $oldSeperator;

    @theVersionArray = split(/\./, $versiondata{$keyName});
    for ($i = 0; $i < 3; $i++) {
        if(!$theVersionArray[$i]) {
            $theVersionArray[$i] = '0';
        }
    }

    @versionArray = split(/\./, $version);
    
    my $actualVersion;

    for ($i = 0; $i < 3; $i++) {
        if (($theVersionArray[$i] != $versionArray[$i]) or ($i == 2)) {

            $actualVersion = $theVersionArray[$i];
            $version = $versionArray[$i];

            last;
        }
    }

    my $expression = '$actualVersion ' . $operator . ' $version';
    if( eval ($expression) )
    {
        return 1;
    }
    else
    {
        return 0;
    }

}

#######################################

my $runnerPID = getppid();
my $installerPID = "";
my $uid = "";
my $uname = "";

open( PSOUT, "/bin/ps -axww -o pid,ppid -p $runnerPID |" );
while( <PSOUT> ) {
	my @fields = split '\s+', $_;
	if ("$fields[1]" eq "$runnerPID") {
		$installerPID = $fields[2];
	}
}
close( PSOUT );

if ("$installerPID" ne "") {
	open( PSOUT, "/bin/ps -axww -o pid,ruid -p $installerPID |" );
	while( <PSOUT> ) {
		my @fields = split '\s+', $_;
		if ("$fields[1]" eq "$installerPID") {
			$uid = $fields[2];
		}
	}
	close( PSOUT );
}

if ("$uid" ne "") {
	open( PSOUTA, "/usr/bin/id -p $uid |" );
	while( <PSOUTA> ) {
		my @fields = split '\s+', $_;
		if ("$fields[0]" eq "uid") {
			$uname = $fields[1];
		}
	}
	close( PSOUTA );
}

###################################################################

# Remove old unneeded quicktime pieces
my $CLEANUP_SCRIPT = "$ARGV[0]" . "/Contents/Resources/cleancruft";
$cmd = "\"$CLEANUP_SCRIPT\" \"$ARGV[0]\" \"$ARGV[1]\" \"$ARGV[2]\" \"$ARGV[3]\"";
system( "$cmd" );

########	
# Stuff the installation info into the preferences for QT Updates
my $INSTALL_INFO= "$ARGV[0]" . "/Contents/Resources/installinfo";
my $cmd = "\"$INSTALL_INFO\" -postinstall -volume \"$ARGV[2]\" -type full";
#print STDOUT "INSTALL_INFO: $cmd\n";
system( "$cmd" );

########

# Configure the browser mime types based on the installed QuickTime
my $CONFIGURE_MIME_TYPES= "$ARGV[0]" . "/Contents/Resources/ConfigureMimeTypes";
my $cmd = "\"$CONFIGURE_MIME_TYPES\" -rescan -verbose";
if ("$uname" ne "") {
	$cmd = "sudo -u $uname \"$CONFIGURE_MIME_TYPES\" -rescan -verbose";
}
#print STDOUT "CONFIGURE_MIME_TYPES: $cmd\n";
system( "$cmd" );

########







	
###########


#if it is panther than remove the Tiger files.
my $Delete = "$ARGV[0]" . "/Contents/Resources/deleteomatic";
my $PANTHER_DELETED_FILES="$ARGV[0]" . "/Contents/Resources/panthercleanup-list";
if(CheckVersion( "$SYSVERS", "10.4", "ProductVersion", "<" )) {
	system ("$Delete \"$ARGV[2]\" \"$PANTHER_DELETED_FILES\""); 
}

####cleanup is not absent in SEED package#######

my $CLEAN_SCRIPT = "$ARGV[0]" . "/Contents/Resources/cleanup";
$cmd = "\"$CLEAN_SCRIPT\" \"$ARGV[0]\" \"$ARGV[1]\" \"$ARGV[2]\" \"$ARGV[3]\"";
if(-e $CLEAN_SCRIPT) {
	system( "$cmd" );
}
exit(0);
